package cn.com.gridinfo.sooc.core.expand.callname.activity;

import android.os.Bundle;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;

import com.jeremy.arad.utils.StringUtils;
import com.squareup.picasso.Picasso;

import butterknife.ButterKnife;
import butterknife.InjectView;
import butterknife.OnClick;
import cn.com.gridinfo.sooc.R;
import cn.com.gridinfo.sooc.base.BaseSoocActivity;
import cn.com.gridinfo.sooc.core.expand.callname.common.CodeConst;
import cn.com.gridinfo.sooc.core.expand.callname.dao.CallNameDao;
import cn.com.gridinfo.sooc.utils.MessageUtils;

/**
 * 签到状态
 *
 * @author yingchunli
 */
public class SigninStateActivity extends BaseSoocActivity {

    @InjectView(R.id.studentName_signinstate)
    TextView studentName;//学生姓名
    @InjectView(R.id.studentClass_signinstate)
    TextView studentClass;//学生班级
    @InjectView(R.id.state_signninstate)
    TextView stateSign;//签到状态
    @InjectView(R.id.setquexiBtn)
    TextView setquexiBtn;//设为缺席按钮
    @InjectView(R.id.fangshi_signinstate)
    TextView fangshiSigninstate;//签到方式
    @InjectView(R.id.signintime)
    TextView signintime;//签到时间
    @InjectView(R.id.image_signinstate)
    ImageView imageSign;//签到图片
    CallNameDao dao;
    String qdfs;//签到方式
    String path;//图片路径

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.expand_signin_signinstate);
        ButterKnife.inject(this);
        dao = new CallNameDao(this);
        initView();//初始化界面数据
    }

    /**
     * 初始化界面数据
     */
    private void initView() {
        //设置学生姓名和学号
        studentName.setText(getIntent().getStringExtra("xm") + "(" + getIntent().getStringExtra("xh") + ")");
        studentClass.setText(getIntent().getStringExtra("bj"));//班级

        //签到方式
        qdfs = getIntent().getStringExtra("qdfs");

        //获取学生的签到状态  1:已到    2:缺席
        if (getIntent().getStringExtra("qdzt").equals("1")) {
            stateSign.setText("到席");
            setquexiBtn.setText("设为缺席");

        } else {
            stateSign.setText("缺席");
            setquexiBtn.setText("设为到席");
        }

        if (qdfs.equals("0")) {
            fangshiSigninstate.setText("签到方式: 输入课堂号");
        } else if (qdfs.equals("1")) {
            fangshiSigninstate.setText("签到方式: 扫码");
        } else {
            fangshiSigninstate.setText("签到方式: 补签");
            if (getIntent().getStringExtra("qdzt").equals("1")) {
                stateSign.setText("到席(补签)");
            } else {
                stateSign.setText("缺席");
            }
        }
        //签到时间
        signintime.setText("签到时间: " + getIntent().getStringExtra("qdsj"));//签到时间
        //设置签到的图片
        path = getIntent().getStringExtra("pic");
        if (StringUtils.isNull(path)) {
            //没有图片路径时,表示是补签，设置默认补签图片
            imageSign.setImageResource(R.mipmap.expand_callname_buqian_big);
        } else {
            //有图片路径时
            Picasso.with(this).load(path).error(R.mipmap.signstate_zhan).placeholder(R.mipmap.signstate_zhan).resize(980, 914).into(imageSign);
        }
    }

    //请求成功
    @Override
    public void onRequestSuccess(int requestCode) {
        try {
            if (dao.getStatus().equals("0")) {
                switch (requestCode) {
                    case CodeConst.EXPAND_BUQIAN:
                        //补签成功，设为缺席成功
                        if (setquexiBtn.getText().toString().trim().equals("设为缺席")) {

                            MessageUtils.showRightImageShortToast(this, "设为缺席成功");
                            setquexiBtn.setText("设为到席");
                            stateSign.setText("缺席");
                        } else {

                            MessageUtils.showRightImageShortToast(this, "补签成功");
                            setquexiBtn.setText("设为缺席");
//                            if (!path.equals("")) {
                                stateSign.setText("到席(补签)");
//                            } else {
//                                stateSign.setText("到席");
//                            }

                            //设置签到方式
                            fangshiSigninstate.setText("签到方式: 补签");
                            //设置签到时间
                            signintime.setText("签到时间: " + dao.getSignTime());
                        }
                        break;
                }
            } else {
                MessageUtils.showImageShortToast(this, dao.getStatusCode());
            }
        } catch (Exception e) {
            MessageUtils.showImageShortToast(this, "请求数据异常");
        }

    }

    /**
     * 设为缺席按钮的点击事件
     */
    @OnClick(R.id.setquexiBtn)
    void onClick() {
        if (setquexiBtn.getText().equals("设为缺席")) {
            //设为缺席
            dao.buQian(getIntent().getStringExtra("ktid"), getIntent().getStringExtra("kcid"), getIntent().getStringExtra("xh"), "0", CodeConst.EXPAND_BUQIAN);
        } else {
            //补签
            dao.buQian(getIntent().getStringExtra("ktid"), getIntent().getStringExtra("kcid"), getIntent().getStringExtra("xh"), "1", CodeConst.EXPAND_BUQIAN);
        }
    }

    @Override
    public String setupToolBarTitle() {
        return "签到状态";
    }

    @Override
    public boolean setupToolBarLeftButton(ImageView leftButton) {
        return true;
    }

    /**
     * toolbar回退键
     */
    @OnClick(R.id.toolbar_leftbtn)
    void onToolbarLeftClick() {
        finish();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        setResult(RESULT_OK);
    }
}
